/**
 * 基本信息标签页相关代码
 */
var jbxx = (function() {

	var cache = {};
	var customerId = qmsk.getQueryValue("customerId");
	var memberCardNo = qmsk.getQueryValue("memberCardNo");
	var $saveButton = $("<div class='BottomBtn'><input type='button' value='保存信息'></div>");

	function clearInput(id) {
		$("#" + id).find("input[type='text']").val('');
		$("#" + id).find("input[type='checkbox']").val('');
		$("#" + id).find("input[type='radio']").val('');
		$("#" + id).find("select").val('');
		$("#" + id).find("textarea").val('');
	}

	function disabledInput(id) {
		$("#" + id).find("input[type='text']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='checkbox']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='radio']").attr('disabled', 'disabled');
		$("#" + id).find("input[type='password']").attr('disabled', 'disabled');
		$("#" + id).find("select").attr('disabled', 'disabled');
		$("#" + id).find("textarea").attr('disabled', 'disabled');
	}

	function removeDisabledInput(id) {
		$("#" + id).find("input[type='text']").removeAttr('disabled');
		$("#" + id).find("input[type='checkbox']").removeAttr('disabled');
		$("#" + id).find("input[type='radio']").removeAttr('disabled');
		$("#" + id).find("input[type='password']").removeAttr('disabled');
		$("#" + id).find("select").removeAttr('disabled');
		$("#" + id).find("textarea").removeAttr('disabled');
	}

	function loadInfo() {
		loadCustomerType();
		loadProvinced();
		loadIndustryType();
		loadCustomerInfo(customerId);
		loadywblfwInfo(customerId);

		if (qmsk.isNotEmpty(memberCardNo)) {
			loadMemberInfo(memberCardNo);
		}
		disabledInput("jbxx_hyxx");
		disabledInput("jbxx_ywxx");
		disabledInput("jbxx_blxx");
		disabledInput("jbxx_fwxx");

	}

	/**
	 * 加载客户的基本信息
	 * @param {Object} customerId
	 */
	function loadCustomerInfo(customerId) {
		qmsk.get('../customer/find/customerInfo/' + customerId, function(response) {

			var industryTypeID = response.getValue("industryTypeID");
			var industryTypeName = response.getValue("industryTypeName");
			$("#industryTypeID").empty().append("<option value='" + industryTypeID + "'>" + industryTypeName + "</option>");

			var location = response.getValue("location") + "";
			var province = location.substring(0, 2) + '0000';
			var city = location.substring(0, 4) + '00';
			var country = location;

			$("#jbxx_grxx").find("input[type=text]").val('');

			var data = response.getObject();
			qmsk.bindBean(data);

			$("#provinceId").val(province);
			loadCity();
			$("#cityId").val(city);
			loadCountry();
			$("#countryId").val(country);

			disabledInput("jbxx_grxx");
			$("#jbxx_grxx_edit").unbind('click').bind('click', function() {
				$saveButton.remove();
				$(this).parents(".columns").append($saveButton);
				removeDisabledInput("jbxx_grxx");
				disabledInput("jbxx_hyxx");
				disabledInput("jbxx_ywxx");
				disabledInput("jbxx_blxx");
				disabledInput("jbxx_fwxx");

				$saveButton.unbind('click').bind('click', function() {
					var heads = ["customerName", "sex", "IDNumber", "mp", "tel", "birthday", "email", "fax", "countryId", "address", "zipCode", "industryTypeID", "companyName", "companyDuty", "degree", "marital", "customerType", "password", "memo", "customerGroup"];
					var data = qmsk.collectionData(heads);
					qmsk.post('../customer/update/customerInfo/' + customerId, data, function() {
						artSuccess("更新成功！");
						$saveButton.remove();
						disabledInput("jbxx_grxx");
					});
				});
			});

		});
	}

	/**
	 * 加载会员的基本信息
	 * @param {Object} memberCardNo
	 */
	function loadMemberInfo(memberCardNo) {
		var shopId = null;
		var memberId = null;
		qmsk.get('../customer/find/memberInfo/' + memberCardNo, function(response) {

			shopId = response.getValue("shopID");
			memberId = response.getValue("memberId");
			var shopName = response.getValue("shopName");
			$("#shopID").empty();

			$("#shopID").append("<option value='" + shopId + "'>" + shopName + "</option>");

			qmsk.ansyInitSelect({
				method : 'GET',
				url : '../memberLevel/find/memberLevelList/0/10',
				data : {
					shopId : shopId
				},
				id : "levelID",
				heads : ["ID", "Name"]
			});

			$("#jbxx_hyxx").find("input[type=text]").val('');

			qmsk.bindData(response);

			disabledInput("jbxx_hyxx");
			$("#jbxx_hyxx_edit").unbind('click').bind('click', function() {
				$saveButton.remove();
				$(this).parents(".columns").append($saveButton);
				$("#payPassword").removeAttr('disabled', 'disabled');
				$("#memberMemo").removeAttr('disabled', 'disabled');

				disabledInput("jbxx_grxx");
				disabledInput("jbxx_ywxx");
				disabledInput("jbxx_blxx");
				disabledInput("jbxx_fwxx");

				$saveButton.unbind('click').bind('click', function() {
					//"levelID", "status","createFileTime"
					var heads = ["payPassword", "memberMemo"];
					var data = qmsk.collectionData(heads);
					data.shopId = shopId;
					data.memberId = memberId;
					qmsk.post('../customer/update/memberInfo/' + memberCardNo, data, function() {
						artSuccess("更新成功！");
						$saveButton.remove();
						loadMemberInfo(memberCardNo);
					});
				});
			});

		});
	}

	function loadywblfwInfo(customerId) {
		qmsk.get('../customer/find/customerywblfwInfo/' + customerId, function(response) {
			var heads = ["getlicenseDate", "hopeConcactWay", "maritalDate", "hopeConcactTime", "createFileTime", "customerSourceId", "custSourceName", "userId", "userName", "recommendMan", "recommendManName", "salesConsultant", "salesConsultantName", "serviceConsultant", "serviceConsultantName", "customerManager", "customerManagerName", "customerServiceManager", "customerServiceManagerName"];
			var data = response.getObject(heads);
			data.createFileTime1 = data.createFileTime;
			$("#customerSourceId").empty().append("<option value='" + data.customerSourceId + "'>" + data.custSourceName + "</option>");
			qmsk.bindBean(data);

			disabledInput("jbxx_ywxx");
			disabledInput("jbxx_blxx");
			disabledInput("jbxx_fwxx");

			$("#jbxx_ywxx_edit").unbind('click').bind('click', function() {
				$saveButton.remove();
				$(this).parents(".columns").append($saveButton);
				removeDisabledInput("jbxx_ywxx");
				disabledInput("jbxx_grxx");
				disabledInput("jbxx_hyxx");
				disabledInput("jbxx_blxx");
				disabledInput("jbxx_fwxx");

				$saveButton.unbind('click').bind('click', updateyw);
			});

			$("#jbxx_blxx_edit").unbind('click').bind('click', function() {
				$saveButton.remove();
				$(this).parents(".columns").append($saveButton);
				removeDisabledInput("jbxx_blxx");
				disabledInput("jbxx_grxx");
				disabledInput("jbxx_ywxx");
				disabledInput("jbxx_hyxx");
				disabledInput("jbxx_fwxx");

				$saveButton.unbind('click').bind('click', updatebl);
			});

			$("#jbxx_fwxx_edit").unbind('click').bind('click', function() {
				$saveButton.remove();
				$(this).parents(".columns").append($saveButton);
				removeDisabledInput("jbxx_fwxx");
				disabledInput("jbxx_grxx");
				disabledInput("jbxx_ywxx");
				disabledInput("jbxx_blxx");
				disabledInput("jbxx_hyxx");

				$saveButton.unbind('click').bind('click', updatefw);
			});

		});
	}

	function updateyw() {
		var ids = ["getlicenseDate", "hopeConcactWay", "maritalDate", "hopeConcactTime"];
		var data = qmsk.collectionData(ids);
		qmsk.post('../customer/update/customerYwInfo/' + customerId, data, function() {
			artSuccess('更新成功！');
			$saveButton.remove();
			loadywblfwInfo(customerId);
		});
	}

	function updatebl() {
		var ids = ["customerSourceId", "userId", "recommendMan"];
		var data = qmsk.collectionData(ids);
		qmsk.post('../customer/update/customerBlInfo/' + customerId, data, function() {
			artSuccess('更新成功！');
			$saveButton.remove();
			loadywblfwInfo(customerId);
		});
	}

	function updatefw() {
		var ids = ["salesConsultant", "serviceConsultant", "customerManager", "customerServiceManager"];
		var data = qmsk.collectionData(ids);
		qmsk.post('../customer/update/customerFwInfo/' + customerId, data, function() {
			artSuccess('更新成功！');
			$saveButton.remove();
			loadywblfwInfo(customerId);
		});
	}

	function loadIndustryType() {
		$("#industryTypeID").unbind('click');
		$("#industryTypeID").bind('click', function() {
			initSelecter({
				url : '../industryType/find/opt',
				heads : ["Code", "Name"],
				titles : ["编码", "名称"],
				zindex : 500,
				callBack : function(response, row) {
					var id = response.getValue("id", row);
					var name = response.getValue("Name", row);
					qmsk.initSelect({
						datas : [[id, name]],
						id : "industryTypeID"
					});
				}
			});
		});
	}

	//加载省份
	function loadProvinced() {
		var provinceId = "provinceId";
		$("#" + provinceId).unbind('change');
		$("#" + provinceId).bind('change', function() {
			loadCity();
			$("#countryId").empty().attr('disabled', 'disabled');
		});
		qmsk.ansyInitSelect({
			id : provinceId,
			url : '../location/province',
			heads : ['code', 'name'],
			defaults : {
				key : '选择省'
			}
		});
	}

	/**
	 * 选择好省后加载市
	 */
	function loadCity() {
		var cityId = "cityId";
		$("#" + cityId).unbind('change');
		$("#" + cityId).bind('change', loadCountry);
		var code = $("#provinceId").val();
		if (code !== "选择省" && qmsk.isNotEmpty(code)) {
			$("#" + cityId).removeAttr('disabled');
			qmsk.ansyInitSelect({
				id : 'cityId',
				url : '../location/city/' + code,
				heads : ['code', 'name'],
				defaults : {
					key : '选择市'
				}
			});
		} else {
			$("#" + cityId).empty().attr('disabled', 'disabled');
		}
	}

	/**
	 * 选择号市后 加载区县
	 */
	function loadCountry() {
		var countryId = "countryId";
		$("#" + countryId).removeAttr('disabled');
		var code = $("#cityId").val();
		if (qmsk.isNotEmpty(code) && code != '选择市') {
			qmsk.ansyInitSelect({
				id : countryId,
				url : '../location/countries/' + code,
				heads : ['code', 'name']
			});
		} else {
			$("#" + countryId).empty().attr('disabled', 'disabled');
		}
	}

	function userSelecter(self, dutyName) {

		var map = {
			salesConsultant : '销售顾问',
			serviceConsultant : '服务顾问',
			customerManager : '客户经理',
			customerServiceManager : '客服经理',
			recommendMan : ''
		};

		$(self).blur();
		initSelecter({
			method : 'get',
			url : '../user/find/userSelecter',
			data : {
				dutyName : map[dutyName]
			},
			heads : ["customerName", "loginName"],
			titles : ["用户名称", "登录名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('customerId', row);
				var name = response.getValue('customerName', row);
				$(self).val(name);
				$("#" + dutyName).val(id);
			}
		});
	}


	window.userSelecter = userSelecter;

	/**
	 * 加载品牌信息
	 */
	function loadCustomerSource(self) {
		$(self).blur();
		initSelecter({
			method : 'get',
			url : '../customerSource/find/opt',
			heads : ["id", "name"],
			titles : ["来源Id", "来源名称"],
			zindex : 500,
			callBack : function(response, row) {
				var id = response.getValue('id', row);
				var name = response.getValue('name', row);
				var option = $("<option value='" + id + "'>" + name + "</option>");
				$(self).empty().append($(option));
			}
		});
	}


	window.loadCustomerSource = loadCustomerSource;

	/**
	 * 加载客户类型
	 */
	function loadCustomerType() {
		qmsk.ansyInitSelect({
			method : 'GET',
			url : '../custType/find/opt/0/99',
			id : "customerType",
			heads : ["id", "name"]
		});
	}

	return {
		init : function() {
			loadInfo();
		}
	};

})();
